package com.guli.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guli.commonUtils.JwtUtils;
import com.guli.commonUtils.MD5;
import com.guli.serviceBase.Entity.GuliExcepiton;
import com.guli.ucenter.entity.UcenterMember;
import com.guli.ucenter.entity.vo.LoginCodeVO;
import com.guli.ucenter.entity.vo.LoginPasswordVO;
import com.guli.ucenter.entity.vo.MemberInfoVO;
import com.guli.ucenter.entity.vo.RegisterMemberVO;
import com.guli.ucenter.mapper.UcenterMemberMapper;
import com.guli.ucenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author zxc
 * @since 2023-04-13
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String ,String> redisTemplate;

    // 具体实现 : 根据手机号和密码进行登录
    @Override
    public String loginByPassword(LoginPasswordVO loginPasswordVO) {
        // 1.获取登录表单中所传递过来的手机号和密码（前端进行限定，不能为空;）
        String mobile = loginPasswordVO.getMobile();
        String password = loginPasswordVO.getPassword();

        // 2.根据手机号，去查询对应的用户信息
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile" , mobile);
        UcenterMember member = this.getOne(queryWrapper);
        // 该用户信息不存在的情况 ：
        if(member == null){
            throw new GuliExcepiton(20001 , "该用户不存在！！！");
        }
        // 3.判断密码是否正确（需要经过MD5加密）
        if(!MD5.encrypt(password).equals(member.getPassword())){
            throw new GuliExcepiton(20001 , "密码不正确！！！");
        }
        // 4.校验该用户是否被禁用
        if(member.getIsDisabled()){
            throw new GuliExcepiton(20001 , "该用户被禁用！！！");
        }
        // 5.获取到该用户所对应的JwtToken;
        String token = JwtUtils.getJwtToken(member.getId(), member.getNickname());
        return token;
    }
    // 具体实现 :根据手机号和验证码进行登录
    @Override
    public String loginByCode(LoginCodeVO loginCodeVO) {
        // 1.获取登录表单中所传递过来的手机号和密码（前端进行限定，不能为空;）
        String mobile = loginCodeVO.getMobile();
        String code = loginCodeVO.getCode();

        // 2.根据手机号，去查询对应的用户信息
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile" , mobile);
        UcenterMember member = this.getOne(queryWrapper);
        // 该用户信息不存在的情况 ：
        if(member == null){
            throw new GuliExcepiton(20001 , "该用户不存在！！！");
        }
        // 3.判断验证码是否正确（需要经过MD5加密）

        if(!code.equals(redisTemplate.opsForValue().get(mobile))){
            throw new GuliExcepiton(20001 , "验证码不正确！！！");
        }
        // 4.校验该用户是否被禁用
        if(member.getIsDisabled()){
            throw new GuliExcepiton(20001 , "该用户被禁用！！！");
        }
        // 5.获取到该用户所对应的JwtToken;
        String token = JwtUtils.getJwtToken(member.getId(), member.getNickname());
        return token;
    }

    @Override
    public boolean registerMember(RegisterMemberVO memberVO) {
        // 1.获取注册表单中所传递过来的数据（前端页面，限制其不能为空;）
        String mobile = memberVO.getMobile();
        String name = memberVO.getNickname();
        String code = memberVO.getCode();
        String password = memberVO.getPassword();

        // 2.根据手机号，进行查询，若是存在该用户，则无法注册成功;
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile" , mobile);
        int count = this.count(queryWrapper);
        // 即，所要注册的用户已经存在（每个手机号有且仅能够注册一个用户;）
        if(count > 0){
            throw new GuliExcepiton(20001 , "该手机已经注册过，无法注册！！！");
        }

        // 3.校验验证码是否正确
        if(!code.equals(redisTemplate.opsForValue().get(mobile))){
            throw new GuliExcepiton(20001 , "验证码错误！！！");
        }
        // 4.校验通过后，该注册的用户信息被添加到数据库中;
        UcenterMember member = new UcenterMember();
        member.setNickname(name);
        member.setMobile(mobile);
        member.setPassword(MD5.encrypt(password));
        // 设置默认的用户头像
        member.setAvatar("https://edu-college-zxc.oss-cn-beijing.aliyuncs.com/zxc-gif/picture/210F2130512J47-0-lp.jpg");
        member.setIsDisabled(false);
        boolean isRegister = this.save(member);
        return isRegister;
    }

    @Override
    public MemberInfoVO getMemberInfo(String memberId) {
        // 1.根据会员id，查询到对应的会员信息;
        UcenterMember member = this.getById(memberId);
        // 2.新建MemberInfoVO对象，用于封装所查询到的会员数据;
        MemberInfoVO memberInfo = new MemberInfoVO();
        BeanUtils.copyProperties(member , memberInfo);
        return memberInfo;
    }

    @Override
    public UcenterMember getMemberByOpenId(String openId) {
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid" , openId);
        UcenterMember member = this.getOne(queryWrapper);
        return member;
    }

    @Override
    public Integer countRegisterMembers(String day) {
        Integer sum = baseMapper.countRegisterMembers(day);
        return sum;
    }
}
